/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.xxl.adapter.entity;

import cn.star.framework.strategy.IdStrategyAutoincrementValue;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.xxl.adapter.core.XxlEntity;
import com.xxl.job.admin.core.model.XxlJobInfo;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Transient;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.GenericGenerator;

/**
 * XxlJobInfo
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Entity
@Table(name = "xxl_job_info")
@org.hibernate.annotations.Table(appliesTo = "xxl_job_info", comment = "任务表")
public class JobInfo extends XxlJobInfo implements XxlEntity {
  /** 注释 */
  @Id
  @Column(name = "id", length = 11, nullable = false)
  @GeneratedValue(generator = IdStrategyAutoincrementValue.STRATEGY)
  @GenericGenerator(
      name = IdStrategyAutoincrementValue.STRATEGY,
      strategy = IdStrategyAutoincrementValue.STRATEGY)
  @JsonFormat(shape = Shape.STRING)
  @Comment(value = "主键")
  private int id;

  /** 与外部业务关联的key */
  @Column(name = "key_", length = 64, unique = true)
  private String key;

  /** 执行器主键ID */
  @Column(name = "job_group", length = 11, nullable = false)
  @Comment(value = "执行器主键Id")
  private int jobGroup;

  /** 注释 */
  @Column(name = "job_desc", length = 255, nullable = false)
  @Comment(value = "任务描述")
  private String jobDesc;

  /** 注释 */
  @Column(name = "add_time")
  @Comment(value = "添加时间")
  private Date addTime;

  /** 注释 */
  @Column(name = "update_time")
  @Comment(value = "更新时间")
  private Date updateTime;

  /** 作者 */
  @Column(name = "author", length = 64)
  @Comment(value = "作者")
  private String author;

  /** 报警邮件 */
  @Column(name = "alarm_email", length = 255)
  @Comment(value = "报警邮件")
  private String alarmEmail;

  /** 调度类型 */
  @Column(name = "schedule_type", length = 50, nullable = false)
  @Comment(value = "调度类型")
  private String scheduleType = "NONE";

  /** 调度配置，值含义取决于调度类型 */
  @Column(name = "schedule_conf", length = 128)
  @Comment(value = "调度配置，值含义取决于调度类型")
  private String scheduleConf;

  /** 调度过期策略 */
  @Column(name = "misfire_strategy", length = 50, nullable = false)
  @Comment(value = "调度过期策略")
  private String misfireStrategy = "DO_NOTHING";

  /** 执行器路由策略 */
  @Column(name = "executor_route_strategy", length = 50)
  @Comment(value = "执行器路由策略")
  private String executorRouteStrategy;

  /** 执行器任务handler */
  @Column(name = "executor_handler", length = 255)
  @Comment(value = "执行器任务handler")
  private String executorHandler;

  /** 执行器任务参数 */
  @Column(name = "executor_param", length = 512)
  @Comment(value = "执行器任务参数")
  private String executorParam;

  /** 阻塞处理策略 */
  @Column(name = "executor_block_strategy", length = 50)
  @Comment(value = "阻塞处理策略")
  private String executorBlockStrategy;

  /** 任务执行超时时间，单位秒 */
  @Column(name = "executor_timeout", length = 11, nullable = false)
  @Comment(value = "任务执行超时时间，单位秒")
  private int executorTimeout = 0;

  /** 失败重试次数 */
  @Column(name = "executor_fail_retry_count", length = 11, nullable = false)
  @Comment(value = "失败重试次数")
  private int executorFailRetryCount = 0;

  /** GLUE类型 */
  @Column(name = "glue_type", length = 50, nullable = false)
  @Comment(value = "Glue类型")
  private String glueType;

  /** GLUE源代码 */
  @Lob
  @Column(name = "glue_source")
  @Comment(value = "Glue源代码")
  private String glueSource;

  /** GLUE备注 */
  @Column(name = "glue_remark", length = 128)
  @Comment(value = "Glue备注")
  private String glueRemark;

  /**
   * GLUE更新时间<br>
   * 主数据广播执行时报空指针<br>
   *
   * <pre>
   *   XxlJobTrigger#processTrigger
   *   triggerParam.setGlueUpdatetime(jobInfo.getGlueUpdatetime().getTime());
   * </pre>
   *
   * <pre style='color: red;'>
   *   java.lang.NullPointerException: null
   *   at com.xxl.job.admin.core.trigger.XxlJobTrigger.processTrigger(XxlJobTrigger.java:137)
   *   at com.xxl.job.admin.core.trigger.XxlJobTrigger.trigger(XxlJobTrigger.java:83)
   *   at com.xxl.job.admin.core.thread.JobTriggerPoolHelper$3.run(JobTriggerPoolHelper.java:95)
   *   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
   *   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
   *   at java.lang.Thread.run(Thread.java:748)
   * </pre>
   */
  @Column(name = "glue_updatetime")
  @Comment(value = "Glue更新时间")
  private Date glueUpdatetime;

  /** 子任务ID，多个逗号分隔 */
  @Column(name = "child_jobid", length = 255)
  @Comment(value = "子任务Id，多个逗号分隔")
  private String childJobId;

  /** 调度状态：0-停止，1-运行 */
  @Column(name = "trigger_status", length = 4)
  @Comment(value = "调度状态：0-停止，1-运行")
  private int triggerStatus = 0;

  /** 上次调度时间 */
  @Column(name = "trigger_last_time", length = 13)
  @Comment(value = "上次调度时间")
  private long triggerLastTime = 0L;

  /** 下次调度时间 */
  @Column(name = "trigger_next_time", length = 13)
  @Comment(value = "下次调度时间")
  private long triggerNextTime = 0L;

  /** 执行器名称 */
  @Transient private String executorName;
}

